package com.gym.tygglxt.web.controller;

import com.gym.tygglxt.mapper.EquipmentRentCustomMapper;
import com.gym.tygglxt.mapper.EquipmentServiceCustomMapper;
import com.gym.tygglxt.model.EventManage;
import com.gym.tygglxt.model.PageResult;
import com.gym.tygglxt.pojo.*;
import com.gym.tygglxt.service.EquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/equ")
public class EquipmentController {

    @Autowired
    private EquipmentService equipmentService;

    @RequestMapping("/query")
    public ModelAndView queryEqu() {
        System.out.println("查询器材");
        ModelAndView modelAndView = new ModelAndView();

        List<String> equTypeList = equipmentService.getAllEquType();

        modelAndView.addObject("equTypeList", equTypeList);
        modelAndView.setViewName("/equipment/equQuery");
        return modelAndView;
    }

    @RequestMapping("/queryPrice")
    public ModelAndView queryPrice() {
        System.out.println("查询收费标准");
        ModelAndView modelAndView = new ModelAndView();

        List<String> equTypeList = equipmentService.getAllEquType();

        modelAndView.addObject("equTypeList", equTypeList);
        modelAndView.setViewName("/equipment/equPrice");
        return modelAndView;
    }

    @RequestMapping("/queryRent")
    public ModelAndView queryRent() {
        System.out.println("查询租借记录");
        ModelAndView modelAndView = new ModelAndView();

        List<String> equTypeList = equipmentService.getAllEquType();

        modelAndView.addObject("equTypeList", equTypeList);
        modelAndView.setViewName("/equipment/equRentQuery");
        return modelAndView;
    }

    @RequestMapping("/queryService")
    public ModelAndView queryService() {
        System.out.println("查询报修记录");
        ModelAndView modelAndView = new ModelAndView();

        List<String> equTypeList = equipmentService.getAllEquType();

        modelAndView.addObject("equTypeList", equTypeList);
        modelAndView.setViewName("/equipment/equServiceQuery");
        return modelAndView;
    }

    @RequestMapping("/addEqu")
    public String getAddEquPage() {
        System.out.println("新增器材信息界面");
        return "/equipment/addEqu";
    }

    @RequestMapping("/add")
    @ResponseBody
    public ModelAndView addEqu(Equipment equipment) {
        System.out.println("AddEqu");
        equipmentService.insertEquipment(equipment);
        return new ModelAndView("redirect:/equ/query");
    }

    @RequestMapping("/addPrice")
    public String getAddPricePage() {
        System.out.println("新增收费标准信息界面");
        return "/equipment/addPrice";
    }

    @RequestMapping("/priceAdd")
    @ResponseBody
    public ModelAndView addPrice(EquipmentPrice equipmentPrice) {
        System.out.println("AddPrice");
        equipmentService.insertEquPrice(equipmentPrice);
        return new ModelAndView("redirect:/equ/queryPrice");
    }

    @RequestMapping("/editEqu")
    public ModelAndView getEditEquPage(@RequestParam(value = "equId", defaultValue = "-1") int equId) {
        System.out.println("请求器材修改");
        System.out.println(equId);
        if (equId == -1) {
            return new ModelAndView("redirect:/equ/query");
        } else {
            ModelAndView modelAndView = new ModelAndView();

            Equipment equipment = equipmentService.searchEquById(equId);

            List<String> equTypeList = equipmentService.getAllEquType();

            modelAndView.addObject("equTypeList", equTypeList);
            modelAndView.addObject("equipment", equipment);
            modelAndView.setViewName("/equipment/editEqu");
            return modelAndView;
        }
    }

    @RequestMapping("/edit")
    public ModelAndView editEqu(Equipment equipment) {
        System.out.println("EditEqu");
//        System.out.println(equipment.toString());
        equipmentService.updateEqu(equipment);
        return new ModelAndView("redirect:/equ/query");
    }

    @RequestMapping("/editPrice")
    public ModelAndView editEquPrice(EquipmentPrice equipmentPrice) {
        System.out.println("EditEqu");
//        System.out.println(equipmentPrice.toString());
        equipmentService.updateEquPrice(equipmentPrice);
        return new ModelAndView("redirect:/equ/query");
    }

    @RequestMapping("/details")
    public String equDetails() {
        System.out.println("EquDetails");
        return null;
    }

    @RequestMapping("/del")
    public ModelAndView delEqu(@RequestParam(value = "equId", defaultValue = "-1") int equId) {
        System.out.println("DelEqu");
        if (equId != -1) {
            equipmentService.delEquipment(equId);
        }
        return new ModelAndView("redirect:/equ/query");
    }

    @RequestMapping("/delPrice")
    public ModelAndView delPrice(@RequestParam(value = "equType", defaultValue = "-1") String equType) {
        System.out.println("DelEqu");
        if (!equType.equals("-1")) {
            equipmentService.delEquPrice(equType);
        }
        return new ModelAndView("redirect:/equ/queryPrice");
    }

    @RequestMapping("/rent")
    public ModelAndView rentEqu(@RequestParam(value = "equId", defaultValue = "-1") int equId, @RequestParam(value = "userId", defaultValue = "-1") String userId) {
        System.out.println("rentEqu");
        if (equId != -1 && !userId.equals("-1")) {
            equipmentService.rentEqu(equId, userId);
        }
        return new ModelAndView("/equipment/equRentQuery");
    }

    @RequestMapping("/return")
    public ModelAndView returnEqu(@RequestParam(value = "rentId", defaultValue = "-1") int rentId, @RequestParam(value = "userId", defaultValue = "-1") String userId) {
        System.out.println("delRent");
        if (rentId != -1 && !userId.equals("-1")) {
            equipmentService.returnEqu(rentId, userId);
        }
        return new ModelAndView("/equipment/equRentQuery");
    }

    @RequestMapping("/delRent")
    public ModelAndView delRent(@RequestParam(value = "rentId", defaultValue = "-1") int rentId, @RequestParam(value = "userId", defaultValue = "-1") String userId) {
        System.out.println("rentEqu");
        if (rentId != -1 && !userId.equals("-1")) {
            equipmentService.delEquRent(rentId);
        }
        return new ModelAndView("/equipment/equRentQuery");
    }

    @RequestMapping("/service")
    public ModelAndView serviceEqu(@RequestParam(value = "equId", defaultValue = "-1") int equId, @RequestParam(value = "userId", defaultValue = "-1") String userId) {
        System.out.println("serviceEqu");
        if (equId != -1 && !userId.equals("-1")) {
            equipmentService.serviceEqu(equId, userId);
        }
        return new ModelAndView("/equipment/equServiceQuery");
    }

    @RequestMapping("/finish")
    public ModelAndView finishServiceEqu(@RequestParam(value = "serviceId", defaultValue = "-1") int serviceId, @RequestParam(value = "userId", defaultValue = "-1") String userId) {
        System.out.println("finishServiceEqu");
        if (serviceId != -1 && !userId.equals("-1")) {
            equipmentService.finishEqu(serviceId, userId);
        }
        return new ModelAndView("/equipment/equServiceQuery");
    }

    @RequestMapping("allEquList")
    @ResponseBody
    public PageResult<EquipmentCustom> getAllEqu(@RequestParam(value = "equtype", defaultValue = "-1") String equType, @RequestParam(value = "equStatus", defaultValue = "-1") int equStatus, int page, int limit){
        EquipmentCustomExample equipmentCustomExample = new EquipmentCustomExample();
        equipmentCustomExample.setStart((page - 1) * limit);
        equipmentCustomExample.setPageSize(limit);
        PageResult<EquipmentCustom> result = new PageResult<EquipmentCustom>();
        result.setCode(0);

        if (!equType.equals("-1") || equStatus != -1) {
            equipmentCustomExample.setSelectAll(false);
            EquipmentCustomExample.Criteria criteria = equipmentCustomExample.createCriteria();
            if (!equType.equals("-1")) {
                criteria.andEqutypeEqualTo(equType);
            }
            if (equStatus != -1) {
                criteria.andEqustatusEqualTo(equStatus);
            }
        }

        //获取查询数据
        List<EquipmentCustom> list = equipmentService.queryEquByExample(equipmentCustomExample);

        //获取总记录
        int totalCount = equipmentService.queryEquByExample(null).size();
        result.setCount(totalCount);

        result.setData(list);

        return result;
    }

    @RequestMapping("allPriceList")
    @ResponseBody
    public PageResult<EquipmentPrice> getAllPrice(@RequestParam(value = "equtype", defaultValue = "-1") String equType, int page, int limit){
        EquipmentPriceExample equipmentPriceExample = new EquipmentPriceExample();
        equipmentPriceExample.setStart((page - 1) * limit);
        equipmentPriceExample.setPageSize(limit);
        PageResult<EquipmentPrice> result = new PageResult<EquipmentPrice>();
        result.setCode(0);

        if (!equType.equals("-1")) {
            equipmentPriceExample.setSelectAll(false);
            EquipmentPriceExample.Criteria criteria = equipmentPriceExample.createCriteria();
            criteria.andEqutypeEqualTo(equType);
        }

        //获取查询数据
        List<EquipmentPrice> list = equipmentService.queryPriceByExample(equipmentPriceExample);

        //获取总记录
        int totalCount = equipmentService.queryPriceByExample(null).size();
        result.setCount(totalCount);

        result.setData(list);

        return result;
    }

    @RequestMapping("allRentList")
    @ResponseBody
    public PageResult<EquipmentRentCustom> getAllRent(@RequestParam(value = "equtype", defaultValue = "-1") String equType, @RequestParam(value = "rentstatus", defaultValue = "-1") int rentStatus, int page, int limit){
        EquipmentRentCustomExample equipmentRentCustomExample = new EquipmentRentCustomExample();
        equipmentRentCustomExample.setStart((page - 1) * limit);
        equipmentRentCustomExample.setPageSize(limit);
        PageResult<EquipmentRentCustom> result = new PageResult<EquipmentRentCustom>();
        result.setCode(0);

        if (!equType.equals("-1") || rentStatus != -1) {
            equipmentRentCustomExample.setSelectAll(false);
            EquipmentRentCustomExample.Criteria criteria = equipmentRentCustomExample.createCriteria();
            if (!equType.equals("-1")) {
                criteria.andEqutypeEqualTo(equType);
            }
            if (rentStatus != -1) {
                criteria.andRentstatusEqualTo(rentStatus);
            }
        }

        //获取查询数据
        List<EquipmentRentCustom> list = equipmentService.queryEquRent(equipmentRentCustomExample);

        //获取总记录
        int totalCount = equipmentService.queryEquRent(null).size();
        result.setCount(totalCount);

        result.setData(list);

        return result;
    }

    @RequestMapping("allServiceList")
    @ResponseBody
    public PageResult<EquipmentServiceCustom> getAllService(@RequestParam(value = "equtype", defaultValue = "-1") String equType, @RequestParam(value = "servicestatus", defaultValue = "-1") int serviceStatus, int page, int limit){
        EquipmentServiceCustomExample equipmentServiceCustomExample = new EquipmentServiceCustomExample();
        equipmentServiceCustomExample.setStart((page - 1) * limit);
        equipmentServiceCustomExample.setPageSize(limit);
        PageResult<EquipmentServiceCustom> result = new PageResult<EquipmentServiceCustom>();
        result.setCode(0);

        if (!equType.equals("-1") || serviceStatus != -1) {
            equipmentServiceCustomExample.setSelectAll(false);
            EquipmentServiceCustomExample.Criteria criteria = equipmentServiceCustomExample.createCriteria();
            if (!equType.equals("-1")) {
                criteria.andEqutypeEqualTo(equType);
            }
            if (serviceStatus != -1) {
                criteria.andServicestatusEqualTo(serviceStatus);
            }
        }

        //获取查询数据
        List<EquipmentServiceCustom> list = equipmentService.queryEquService(equipmentServiceCustomExample);

        //获取总记录
        int totalCount = equipmentService.queryEquService(null).size();
        result.setCount(totalCount);

        result.setData(list);

        return result;
    }
}
